var qqcs;

$(function () {

    $("#box").height($(window).height()-$("#dddss").height()-2);
    initTb();

});


//初始化图表
function initTb() {
    require.config({
        paths: {
            echarts: echarts2_dist_url
        }
    });

    require(
        [
            'echarts',
            'echarts/chart/pie'
        ],
        function (ec) {

            var result1 = $("#result1").val();
            var result2 = $("#result2").val();

            if(result1){
                result1=JSON.parse(result1);
            }

            if(result2){
                result2=JSON.parse(result2);
            }

            var chart1 = ec.init(document.getElementById('hyje_div'));
            var chart2 = ec.init(document.getElementById('dqje_div'));
            var chart3 = ec.init(document.getElementById('hyhs_div'));
            var chart4 = ec.init(document.getElementById('dqhs_div'));

            window.onresize = function () {
                if (chart1) {
                    chart1.resize();
                }
                if (chart2) {
                    chart2.resize();
                }
                if (chart3) {
                    chart3.resize();
                }
                if (chart4) {
                    chart5.resize();
                }
            };

             var option1 = {
                tooltip: {
                    trigger: 'item',
                    formatter: "{b}:<br/>{c}万元 <br/>占比{d}%"
                },
                calculable: true,
                series: [
                    {
                        name: '外商投资情况-按行业分析(金额)',
                        type: 'pie',
                        radius: '55%',
                        center: ['50%', '50%'],
                        data: getDataArr(result1,'HY_MC',"WSTZ_JE")
                    }
                ]
            };
            chart1.setOption(option1);

            var option2 = {
                tooltip: {
                    trigger: 'item',
                    formatter: "{b}:<br/>{c}万元 <br/>占比{d}%"
                },
                calculable: true,
                series: [
                    {
                        name: '外商投资情况-按地区分析(金额)',
                        type: 'pie',
                        radius: '55%',
                        center: ['50%', '50%'],
                        data: getDataArr(result2,'DQ_MC',"WSTZ_JE")
                    }
                ]
            };
            chart2.setOption(option2);


            var option3 = {
                tooltip: {
                    trigger: 'item',
                    formatter: "{b}:<br/>共计{c}户 <br/>占比{d}%"
                },
                calculable: true,
                series: [
                    {
                        name: '外商投资情况一体化分析-按行业分析(户数)',
                        type: 'pie',
                        radius: '55%',
                        center: ['50%', '50%'],
                        data: getDataArr(result1,'HY_MC',"WSTZ_HS")
                    }
                ]
            };
            chart3.setOption(option3);

            var option4 = {
                tooltip: {
                    trigger: 'item',
                    formatter: "{b}:<br/>共计{c}户 <br/>占比{d}%"
                },
                calculable: true,
                series: [
                    {
                        name: '外商投资情况一体化分析-按地区分析(户数)',
                        type: 'pie',
                        radius: '55%',
                        center: ['50%', '50%'],
                        data: getDataArr(result2,'DQ_MC',"WSTZ_HS")
                    }
                ]
            };
            chart4.setOption(option4);

            $("input[name='dd']").click(function(){
                if(this.value==1){
                    $("#dqje_div").hide();
                    $("#hyhs_div").hide();
                    $("#dqhs_div").hide();
                    $("#hyje_div").show();
                    if (chart1) {
                        chart1.resize();
                    }
                }else if(this.value==2){
                    $("#hyje_div").hide();
                    $("#hyhs_div").hide();
                    $("#dqhs_div").hide();
                    $("#dqje_div").show();
                    if (chart2) {
                        chart2.resize();
                    }
                }else if(this.value==3){
                    $("#dqje_div").hide();
                    $("#hyje_div").hide();
                    $("#dqhs_div").hide();
                    $("#hyhs_div").show();
                    if (chart3) {
                        chart3.resize();
                    }
                }else{
                    $("#dqje_div").hide();
                    $("#hyhs_div").hide();
                    $("#hyje_div").hide();
                    $("#dqhs_div").show();
                    if (chart4) {
                        chart4.resize();
                    }
                }
            });

            $("#dqje_div").hide();
            $("#hyhs_div").hide();
            $("#dqhs_div").hide();

        }
    );
}


function getNameArr(list,str){
    var nameArr = new Array();
    for(var i=0;i<list.length;i++){
        var data = list[i]
        nameArr.push(data[str]);
    }
    return nameArr;
}

function getDataArr(list,strName,strVal){
    var dataArr = new Array();
    for(var i=0;i<list.length;i++){
        var data = list[i]
        dataArr.push({
            name:data[strName],
            value:data[strVal]
        });
    }
    return dataArr;
}